from django.db import models
from django.utils import timezone
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey

from ..base.api.fields import JSONField


class Timeline(models.Model):
    """
    时间线
    """
    "内容类型"
    content_type = models.ForeignKey(ContentType, related_name="content_type_timelines", on_delete=models.CASCADE, )
    "对象id"
    object_id = models.PositiveIntegerField()
    "内容对象"
    content_object = GenericForeignKey('content_type', 'object_id')
    "命名空间"
    namespace = models.CharField(max_length=250, default="default", db_index=True)
    "事件类型"
    event_type = models.CharField(max_length=250, db_index=True)
    "项目"
    project = models.ForeignKey("projects.Project", null=True, on_delete=models.CASCADE,)
    "操作数据"
    data = JSONField()
    "操作数据类型"
    data_content_type = models.ForeignKey(ContentType, related_name="data_timelines", on_delete=models.CASCADE,)
    "创建时间"
    created = models.DateTimeField(default=timezone.now, db_index=True)

    class Meta:
        indexes = [
            models.Index(fields=['namespace', '-created']),
            models.Index(fields=['content_type', 'object_id', '-created']),
        ]
